﻿package
{
	import flash.display.*;
	import flash.events.*;
	import flash.system.*;
	import flash.utils.*;
	
	import com.urbansquall.display.*;
	
	public class Main extends Sprite
	{
		public static const ICON_WIDTH : int = 24;
		public static const ICON_HEIGHT : int = 24;
		public static const ICON_COUNT : int = 60;
		public static const ICONS_PER_ROW : int = 30;
		public static const ROW_COUNT : int = ICON_COUNT / ICONS_PER_ROW;
		public static const DRAW_REPEATS : int = 5;
		
		/* TEST_NAIVE_INSTANTIATION
		 * Naive method of just instantiating bitmap datas as we need them
		 * Memory Used: 4603904
		 */
		public static const TEST_NAIVE_INSTANTIATION : String = "test_naive";
				
		/* TEST_WORST_CASE_SCENARIO
		 * Cloning the bitmap datas for every icon
		 * Memory Used: 5672960
		 */
		public static const TEST_WORST_CASE_SCENARIO : String = "test_worst_case_scenario";
		
		/* TEST_BITMAPLIBRARYMANAGER
		 * Uses the bitmap library to manage bitmap data instantiation
		 * Memory Usage: 3575808
		 */
		public static const TEST_BITMAPLIBRARYMANAGER : String = "test_BitmapLibraryManager";
		
		private var m_iconLib : IconLibrary;
		
		public function Main() : void
		{
			/*
			for( var i : int = 0; i < ICON_COUNT; i++ )
			{
				var index : String = (( i < 9 ) ? ("0" + (i+1) ) : String( i+1 ) );
				trace( "[Embed(source='../assets/icons/001_" + index + ".png')]" );
				trace( "public const Icon_001_" + index + " : Class;" );
			}
			return;
			*/
			
			
			addEventListener( Event.ADDED_TO_STAGE, init );
		}
		
		private function init( a_event : Event = null ) : void
		{
			removeEventListener( Event.ADDED_TO_STAGE, init );
						
			m_iconLib = new IconLibrary();
			
			runTest( TEST_BITMAPLIBRARYMANAGER );
		}
		
		
		private function runTest( a_test : String ) : void
		{
			switch( a_test )
			{
				case TEST_WORST_CASE_SCENARIO:
				{
					testWorstCaseScenario();
				}
				break;
				case TEST_NAIVE_INSTANTIATION:
				{
					testNaiveInstantiation();
				}
				break;
				case TEST_BITMAPLIBRARYMANAGER:
				{
					testBitmapLibraryManager();
				}
				break;
				default:
				{
					trace( "Unknown test: " + a_test );
				}
			}
			
			traceMemoryUsage();
		}
		
		private function testWorstCaseScenario() : void
		{
			var bitmaps : Array = new Array();
			
			for( var z : int = 0; z < DRAW_REPEATS; z++ )
			{
				for( var i : int = 0; i < ICON_COUNT; i++ )
				{
					var index : String = (( i < 9 ) ? ("0" + (i+1) ) : String( i+1 ) );
					bitmaps.push( new Bitmap( (new m_iconLib[ "Icon_001_" + index ]).bitmapData.clone() ) );
				}
			}
			
			drawIcons( bitmaps );
		}
		
		private function testNaiveInstantiation() : void
		{
			var bitmaps : Array = new Array();
			
			for( var z : int = 0; z < DRAW_REPEATS; z++ )
			{
				for( var i : int = 0; i < ICON_COUNT; i++ )
				{
					var index : String = (( i < 9 ) ? ("0" + (i+1) ) : String( i+1 ) );
					bitmaps.push( new m_iconLib[ "Icon_001_" + index ] );
				}
			}
			
			drawIcons( bitmaps );
		}
		
		private function testBitmapLibraryManager() : void
		{
			BitmapLibraryManager.registerBitmapLibrary( m_iconLib );
			
			var bitmaps : Array = new Array();
			
			for( var z : int = 0; z < DRAW_REPEATS; z++ )
			{
				for( var i : int = 0; i < ICON_COUNT; i++ )
				{
					var index : String = (( i < 9 ) ? ("0" + (i+1) ) : String( i+1 ) );
					bitmaps.push( BitmapLibraryManager.createBitmap( "Icon_001_" + index ) );
				}
			}
			
			drawIcons( bitmaps );
		}
		
		private function drawIcons( a_icons : Array ) : void
		{
			var drawY : int = 0;
			
			for( var d : int = 0; d < DRAW_REPEATS; d++ )
			{
				for( var x : int = 0; x < ICONS_PER_ROW; x++ )
				{
					for( var y : int = 0; y < ROW_COUNT; y++ )
					{
						var icon : Bitmap = a_icons.shift();
						icon.x = x * ICON_WIDTH;
						icon.y = drawY + y * ICON_HEIGHT;
						addChild( icon );
					}
				}
				
				drawY += ROW_COUNT * ICON_HEIGHT;
			}
		}
		
		private function traceMemoryUsage( a_event : Event = null ) : void
		{
			trace( "Memory Used: " + System.totalMemory );
		}
	}
	
}